class TextCursor {
    constructor(x = 0, y = 0, width = 2, fill = 'rgba(255,255,0,1)') {
        this.width = width;
        this.fill = fill;
        this.x = x;
        this.y = y;
        this.visible = true;
        this.blink = 60;
    }

    static singleton(...args) {
        this._instance = this._instance || new this(...args);
        return this._instance;
    }

    getHeight(context) {
        let w = context.measureText('w').width;
        return w + w / 6;
    }

    update() {

        // 设置光标闪烁
        if (this.blink < 30) {
            this.visible = true;
        } else {
            this.visible = false;
        }

        this.blink--;

        if (this.blink === 0) {
            this.blink = 60;
        }

    }

    draw(context, ox, oy) {
        if (!this.visible) {
            return;
        }

        context.save();
        context.fillStyle = this.fill;
        context.fillRect(this.x + ox, this.y + oy, this.width, this.getHeight(context));
        context.restore();
    }
}

export { TextCursor }